﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.IO;
using System.IO.IsolatedStorage;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Serialization;

/**
 * 创建时间：2014/11/19 14:29:15
 * 作者：yong.blackcore
 * Email：itblackhole@outlook.com
 * QQ：50404503
 */
namespace ShareFolder.ViewModel
{
    public class MainViewModel : INotifyPropertyChanged
    {
        private ConnectionViewModel _selectedConnection;

        private Settings _settings;

        public Settings AppSettings
        {
            get
            {
                return this._settings;
            }
        }

        public bool IsDataLoaded
        {
            get;
            private set;
        }

        public ObservableCollection<ConnectionViewModel> Items
        {
            get;
            private set;
        }

        public ConnectionViewModel SelectedConnection
        {
            get
            {
                return this._selectedConnection;
            }
            set
            {
                this._selectedConnection = value;
                this.NotifyPropertyChanged("SelectedConnection");
            }
        }

        public MainViewModel()
        {
            this.Items = new ObservableCollection<ConnectionViewModel>();
        }

        public void AddConnection(ConnectionViewModel Item)
        {
            this.Items.Add(Item);
        }

        public void LoadData()
        {
            try
            {
                using (IsolatedStorageFileStream isolatedStorageFileStream = new IsolatedStorageFileStream("Connections.xml", FileMode.Open, IsolatedStorageFile.GetUserStoreForApplication()))
                {
                    List<ConnectionViewModel> connectionViewModels = (List<ConnectionViewModel>)(new XmlSerializer(typeof(List<ConnectionViewModel>))).Deserialize(isolatedStorageFileStream);
                    this.Items.Clear();
                    foreach (ConnectionViewModel connectionViewModel in connectionViewModels)
                    {
                        this.Items.Add(connectionViewModel);
                    }
                }
            }
            catch (Exception exception)
            {
            }
            this.IsDataLoaded = true;
        }

        public void LoadSettings()
        {
            try
            {
                using (IsolatedStorageFileStream isolatedStorageFileStream = new IsolatedStorageFileStream("Settings.xml", FileMode.Open, IsolatedStorageFile.GetUserStoreForApplication()))
                {
                    XmlSerializer xmlSerializer = new XmlSerializer(typeof(Settings));
                    this._settings = (Settings)xmlSerializer.Deserialize(isolatedStorageFileStream);
                }
            }
            catch (Exception exception)
            {
                this._settings = new Settings();
            }
        }

        private void NotifyPropertyChanged(string propertyName)
        {
            PropertyChangedEventHandler propertyChangedEventHandler = this.PropertyChanged;
            if (propertyChangedEventHandler != null)
            {
                propertyChangedEventHandler(this, new PropertyChangedEventArgs(propertyName));
            }
        }

        public void SaveData()
        {
            using (IsolatedStorageFileStream isolatedStorageFileStream = new IsolatedStorageFileStream("Connections.xml", FileMode.Create, IsolatedStorageFile.GetUserStoreForApplication()))
            {
                XmlSerializer xmlSerializer = new XmlSerializer(typeof(List<ConnectionViewModel>));
                List<ConnectionViewModel> connectionViewModels = new List<ConnectionViewModel>();
                foreach (ConnectionViewModel item in this.Items)
                {
                    connectionViewModels.Add(item);
                }
                xmlSerializer.Serialize(isolatedStorageFileStream, connectionViewModels);
                isolatedStorageFileStream.Flush();
            }
        }

        public void SaveSettings()
        {
            using (IsolatedStorageFileStream isolatedStorageFileStream = new IsolatedStorageFileStream("Settings.xml", FileMode.Create, IsolatedStorageFile.GetUserStoreForApplication()))
            {
                (new XmlSerializer(typeof(Settings))).Serialize(isolatedStorageFileStream, this._settings);
                isolatedStorageFileStream.Flush();
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;
    }
}
